package com.mh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mh.dto.vo.R;
import com.mh.dto.vo.TransferVo;
import com.mh.pojo.AssetAccount;
import com.mh.service.AssetAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import java.util.List;

/**
 * 资产账户表(AssetAccount)表控制层
 *
 * @author zmh
 * @since 2025-02-08 09:18:48
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/assetAccounts")
public class AssetAccountController {
    /**
     * 服务对象
     */
    @Autowired
    private AssetAccountService assetAccountService;

    /**
     * 转账
     * @param transferVo 转账参数：转出账户ID， 转入账户ID，转账金额
     * @return ·
     */
    @PostMapping("/transfer")
    R<Boolean> transfer(@RequestBody TransferVo transferVo){
        return assetAccountService.transfer(transferVo);
    }

    /**
     * 根据用户ID获取该用户的所有账户信息
     * @param userId 用户ID
     * @return ·
     */
    @GetMapping("/getAllByUserId/{userId}")
    R<List<AssetAccount>> getAllByUserId(@PathVariable("userId") Integer userId){
         return assetAccountService.getAllByUserId(userId);
    }

    /**
     * 分页查询
     *
     * @param page 查询页数
     * @param size 一页显示条数
     * @return ·
     */
    @GetMapping("/page")
    public R<Page<AssetAccount>> getAllByPage(int page, int size) {
        //执行查询
        return assetAccountService.getAllByPage(page, size);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R<AssetAccount> selectOne(@PathVariable Serializable id) {
        return R.success(this.assetAccountService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param assetAccount 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R<String> insert(@RequestBody AssetAccount assetAccount) {
        return R.success(this.assetAccountService.save(assetAccount) + "");
    }

    /**
     * 修改数据
     *
     * @param assetAccount 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R<String> update(@RequestBody AssetAccount assetAccount) {
        return R.success(this.assetAccountService.updateById(assetAccount) + "");
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") String id) {
        return R.success(this.assetAccountService.removeById(id) + "");
    }
}

